<?php
/**
 *	后台管理系统-核心功能
 */
namespace app\bews\controller;
use app\bews\controller\Base;
use think\facade\Db;
use think\facade\View;

class Bewadmin extends Base{
	# 部门管理
	public function groupinfo(){
		$group = Db::table('bew_admin_user_group')->select();
		View::assign([
			'group' => $group
		]);
		return View::fetch();
	}
	# 部门添加
	public function groupadd(){
		$req = request();
		if($req->isPost()){
			$data['group_name'] = trim(input('post.group_name'));
			if(!$data['group_name']){
				$this->returnCode('90000008');
			}
			$data['status'] = (int)trim(input('post.status'));
			$data['time_add'] = time();
			$menus = input('post.menu/a');
			if($menus){
				$data['rights'] = json_encode(array_keys($menus));
			}
			$res = Db::table('bew_admin_user_group')->insert($data);
			if(!$res){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			$menus = Db::table('bew_admin_sys_menu')->where('status',1)->select();
			$menu = [];
			foreach ($menus as $key => $value) {
				$menu[$value['smid']] = $value;
			}
			$menu = gettreeitemsnew($menu);
			$results = array();
			foreach ($menu as $value) {
				$value['children'] = isset($value['children']) ? formatMenus($value['children']) : false;
				$results[] = $value;
			}
			View::assign([
				'menus' => $results
			]);
			return View::fetch();
		}
	}
	# 部门修改
	public function groupedit(){
		$req = request();
		if($req->isPost()){
			$group_id = (int)trim(input('post.group_id'));
			$data['group_name'] = trim(input('post.group_name'));
			if(!$data['group_name']){
				$this->returnCode('90000008');
			}
			$data['status'] = (int)trim(input('post.status'));
			$menus = input('post.menu/a');
			if($menus){
				$data['rights'] = json_encode(array_keys($menus));
			}else{
				$data['rights'] = '';
			}
			$res = Db::table('bew_admin_user_group')->where('group_id',$group_id)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$group_id = (int)input('get.group_id');
			$group = Db::table('bew_admin_user_group')->where('group_id',$group_id)->find();
			if($group && $group['rights']){
				$group['rights'] = json_decode($group['rights']);
			}

			$menus = Db::table('bew_admin_sys_menu')->where('status',1)->select();
			$menu = [];
			foreach ($menus as $key => $value) {
				$menu[$value['smid']] = $value;
			}

			$menu = gettreeitemsnew($menu);
			$results = array();
			foreach ($menu as $value) {
				$value['children'] = isset($value['children']) ? formatMenus($value['children']) : false;
				$results[] = $value;
			}
			View::assign([
				'group' => $group,
				'menus' => $results
			]);
			return View::fetch();
		}
	}
	# 部门删除
	public function groupdel(){
		$group_id = (int)input('post.group_id');
		$res = Db::table('bew_admin_user_group')->where('group_id',$group_id)->delete();
		if(empty($res)){
			$this->returnCode('91000003');
		}
		$this->returnCode(0);
	}
	# 管理员列表
	public function userinfo(){
		$lists = Db::table('bew_admin_user')->select();
		$group = [];
		$groups = Db::table('bew_admin_user_group')->select();
		foreach ($groups as $key => $value) {
			$group[$value['group_id']] = $value;
		}
		View::assign([
			'lists' => $lists,
			'group' => $group
		]);
		return View::fetch();
	}
	# 管理员添加
	public function useradd(){
		$req = request();
		if($req->isPost()){
			$data['account'] = trim(input('post.account'));
			if(empty($data['account'])){
				$this->returnCode('90000001');
			}
			$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
			if(!preg_match($pattern,$data['account'])){
				$this->returnCode('90000006');
			}
			$item = Db::table('bew_admin_user')->where('account',$data['account'])->find();
			if($item){
				$this->returnCode('90000007');
			}
			$data['name'] = trim(input('post.name'));
			$data['mobile'] = trim(input('post.mobile'));
			$data['qq'] = (int)trim(input('post.qq'));
			$data['group_id'] = (int)input('post.group_id');
			$data['sex'] = (int)(input('post.sex'));
			$data['status'] = (int)(input('post.status'));
			$password = trim(input('post.password'));
			if(empty($data['name'])){
				$this->returnCode('90000002');
			}
			if(empty($data['mobile'])){
				$this->returnCode('90000003');
			}
			if(empty($data['group_id'])){
				$this->returnCode('90000004');
			}
			if(empty($password)){
				$this->returnCode('90000005');
			}else{
			    $data['password'] = md5($password);
			}
			$data['time_add'] = time();
			$data['time_last'] = time();
			$res = Db::table('bew_admin_user')->insert($data);
			if(!$res){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			$group = [];
			$groups = Db::table('bew_admin_user_group')->select();
			foreach ($groups as $key => $value) {
				$group[$value['group_id']] = $value;
			}
			View::assign([
				'group' => $group
			]);
			return View::fetch();
		}
	}
	# 修改管理员
	public function useredit(){
		$req = request();
		if($req->isPost()){
			$uid = (int)trim(input('post.uid'));
			$data['name'] = trim(input('post.name'));
			$data['mobile'] = trim(input('post.mobile'));
			$data['qq'] = (int)trim(input('post.qq'));
			$data['group_id'] = (int)input('post.group_id');
			$data['sex'] = (int)(input('post.sex'));
			$data['status'] = (int)(input('post.status'));
			if(empty($data['name'])){
				$this->returnCode('90000002');
			}
			if(empty($data['mobile'])){
				$this->returnCode('90000003');
			}
			if(empty($data['group_id'])){
				$this->returnCode('90000004');
			}
			// 保存用户
			$res = Db::table('bew_admin_user')->where('uid',$uid)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$uid = (int)input('get.uid');
			// 加载管理员
			$lists = Db::table('bew_admin_user')->where('uid',$uid)->find();
			// 加载角色
			$group = [];
			$groups = Db::table('bew_admin_user_group')->select();
			foreach ($groups as $key => $value) {
				$group[$value['group_id']] = $value;
			}
			View::assign([
				'lists' => $lists,
				'group' => $group
			]);
			return View::fetch();
		}
	}
	# 删除管理员
	public function userdel(){
		$uid = (int)input('post.uid');
		$res = Db::table('bew_admin_user')->where('uid',$uid)->delete();
		if(empty($res)){
			$this->returnCode('91000003');
		}
		$this->returnCode(0);
	}
	# 管理员信息
	public function admininfo(){
		$req = request();
		if($req->isPost()){
			$find = Db::table('bew_admin_user')->where('uid',$this->adminId)->find();
			if(empty($find)){
				$this->returnCode(1,'当前账户不存在');
			}
			$data['name'] = trim(input('post.name'));
			$data['mobile'] = trim(input('post.mobile'));
			$data['qq'] = (int)trim(input('post.qq'));
			$data['sex'] = (int)(input('post.sex'));
			if(empty($data['name'])){
				$this->returnCode('90000002');
			}
			if(empty($data['mobile'])){
				$this->returnCode('90000003');
			}
			// 保存用户
			$res = Db::table('bew_admin_user')->where('uid',$this->adminId)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			return View::fetch();
		}
	}
}